package com.jiandaoshou.fragments;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.jiandaoshou.CaiWuGuanliAct;
import com.jiandaoshou.MyMessageAct;
import com.jiandaoshou.R;
import com.jiandaoshou.RePassWordAct;
import com.jiandaoshou.SettingActivity;
import com.jiandaoshou.TiXianAct;
import com.jiandaoshou.YinHangKaAct;
import com.jiandaoshou.base.BaseFragment;
import com.jiandaoshou.entry.RefreshMoneyEvent;
import com.jiandaoshou.entry.ShopBalanceEntry;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.CommonUtil;
import com.jiandaoshou.utils.JsonUtil;
import com.jiandaoshou.utils.RxBus;
import com.jiandaoshou.utils.UserUtils;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.HashMap;

public class ZhanghuFragment extends BaseFragment implements View.OnClickListener {
    private LinearLayout Linear_ZhanghuTiXian;
    private LinearLayout Linear_YinHangKa;
    private LinearLayout Linear_CaiWuGuanLi;
    private LinearLayout Linear_MyMessage;
    private LinearLayout Linear_Password;
    private TextView Tv_ZhuangHuToatl;
    private XRefreshView xRefreshView;
    private String shopId;
    private ShopBalanceEntry entry;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    float count=0.000f;
                    entry = (ShopBalanceEntry) msg.obj;
                    if (!TextUtils.isEmpty(entry.getShopBalance())&&!TextUtils.isEmpty(entry.getNoOperationApply())) {
                        count=Float.parseFloat(entry.getShopBalance())-Float.parseFloat(entry.getNoOperationApply());

                        Tv_ZhuangHuToatl.setText(CommonUtil.roundDouble(count));
                    }
                    try {
                        UserUtils.saveBalance(getContext(), count);
                    }catch (Exception e){
                        Log.e("ZhanghuFragment", " UserUtils.saveBalance(getContext(), count): "+e.getMessage());
                    }

                    xRefreshView.stopRefresh();
                    break;
                case 1:
                    xRefreshView.stopLoadMore();
                    break;
            }
        }
    };

    @Subscriber(tag = "onRefresh")
    public void refresh(String s){
        GetData();
    }
    @Override
    protected void lazyLoad() {
        GetData();
    }

    private void GetData() {
        HashMap<String, String> map = new HashMap<>();
        shopId = getArguments().getString("shopId", "");
        map.put("strShopID", shopId);
        Http.post(APIDefine.TF_Interface_GetShopBalance, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                ShopBalanceEntry entry = JsonUtil.parser.fromJson(response, ShopBalanceEntry.class);
                Message msg = handler.obtainMessage();
                msg.obj = entry;
                msg.what = 0;
                msg.sendToTarget();
            }
        });
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_my;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        Linear_ZhanghuTiXian = $(R.id.lin_zhanghutixian);
        Linear_YinHangKa = $(R.id.lin_yinhangka);
        Linear_CaiWuGuanLi = $(R.id.lin_caiwuguanli);
        Linear_MyMessage = $(R.id.lin_mymessage);
        Linear_Password = $(R.id.lin_repassword);
        Tv_ZhuangHuToatl = $(R.id.tv_zichantotal);
        xRefreshView=$(R.id.custom_view);
        Linear_ZhanghuTiXian.setOnClickListener(this);
        Linear_YinHangKa.setOnClickListener(this);
        Linear_CaiWuGuanLi.setOnClickListener(this);
        Linear_MyMessage.setOnClickListener(this);
        Linear_Password.setOnClickListener(this);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onRefresh() {
                super.onRefresh();
                GetData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                handler.sendEmptyMessageDelayed(1,1000);
            }
        });

        $(R.id.lin_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingActivity.start(getContext());
            }
        });

        RxBus.getDefault().toObservable(RefreshMoneyEvent.class)
                .subscribe(refreshMoneyEvent -> {
                    if (refreshMoneyEvent.is_refresh) {
                        GetData();
                    }
                });

    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.lin_zhanghutixian:
                intent = new Intent(getActivity(), TiXianAct.class);
                intent.putExtra("count", entry.getShopBalance());
                intent.putExtra("shopId", shopId);
                startActivity(intent);
                break;
            case R.id.lin_yinhangka:
                intent = new Intent(getActivity(), YinHangKaAct.class);
                intent.putExtra("shopId", shopId);
                startActivity(intent);
                break;
            case R.id.lin_caiwuguanli:
                intent = new Intent(getActivity(), CaiWuGuanliAct.class);
                intent.putExtra("shopId",shopId);
                startActivity(intent);
                break;
            case R.id.lin_mymessage:
                intent =new Intent(getActivity(), MyMessageAct.class);
                intent.putExtra("shopId",shopId);
                startActivity(intent);
                break;
            case R.id.lin_repassword:
                intent=new Intent(getActivity(), RePassWordAct.class);
                intent.putExtra("shopId",shopId);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("asdf", "onResume: " );
    }
}
